package com.ay.selfservicemachine.http;

import org.apache.http.conn.ConnectTimeoutException;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public class HttpResultCode {

    /** Failed to data analysis. */
    public static final int ANALYSIS_DATA_FAILED = 1;

    /** Return to null data. */
    public static final int RETURN_DATA_NULL = 2;

    /** Network disconnected */
    public static final int NETWORK_DISCONNECTED = 3;

    /** Network request timeout */
    public static final int NETWORK_TIMEOUT = 5;

    /** Network connection failed */
    public static final int NETWORK_CONNERCTION_FAILED = 6;

    /** OK */
    public static final int REQUEST_OK = 200;
    /** 服务器错误，请重试 */
    public static final int SERVICE_ERROR = 101;
    /** 数据转换失败 */
    public static final int RESPONSE_PARSE_ERROR = 102;
    /** 网络未连接 */
    public static final int NET_UNCONNECTED = 103;
    /** 网络超时 */
    public static final int NET_OVER_TIME = 104;

    public static String handleException(Throwable e) {
        if (e instanceof UnknownHostException || e instanceof ConnectException) {
            return "当前网络未连接,请检查网络ERRORCODE:103";
        } else if (e instanceof SocketTimeoutException) {
            return "服务器响应超时ERRORCODE：200";
        } else if (e instanceof ConnectTimeoutException) {
            return "连接服务器超时ERRORCODE：104";
        } else if (e instanceof SocketException) {
            return "连接异常";
        } else {
            return e.getMessage();
        }
    }

}
